package org.yxjme.exjni_two.v;

import android.content.Context;
import android.hardware.Camera;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import androidx.annotation.NonNull;
import java.io.IOException;

public class CameraSurfaceView extends SurfaceView implements SurfaceHolder.Callback {

    private Camera camera;


    public CameraSurfaceView(Context context) {
        this(context,null);
    }


    public CameraSurfaceView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }


    public CameraSurfaceView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    /**
     * @param camera
     */
    public void init(Camera camera){
        this.camera=camera;
        getHolder().addCallback(this);
    }



    @Override
    public void surfaceCreated(@NonNull SurfaceHolder holder) {
        if (camera!=null){
            try {
                /**
                 * 将预览内容交给surfaceView呈现出来
                 */
                camera.setPreviewDisplay(holder);
                /**
                 * 摄像头预览方向
                 */
                camera.setDisplayOrientation(90);
                /**
                 * 开始预览
                 */
                camera.startPreview();
                /**
                 * 设置对焦
                 */
                camera.autoFocus(new MyAutoFocusCallback());
                /**
                 * 手动聚焦
                 */
                this.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        camera.autoFocus(new MyAutoFocusCallback());
                    }
                });
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }



    @Override
    public void surfaceChanged(@NonNull SurfaceHolder holder, int format, int width, int height) {

    }



    @Override
    public void surfaceDestroyed(@NonNull SurfaceHolder holder) {

    }



    /**
     * 获取 焦点
     */
    public class MyAutoFocusCallback implements Camera.AutoFocusCallback {
        @Override
        public void onAutoFocus(boolean success, Camera camera) {
            Log.v("[onAutoFocus]->","success ="+success);
            if (success) {
                camera.cancelAutoFocus();// 只有加上了这一句，才会自动对焦
                doAutoFocus();
            }
        }
    }


    /**
     * 循环对焦的方式
     */
    private void doAutoFocus() {
        final Camera.Parameters[] parameters = {camera.getParameters()};
        parameters[0].setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
        camera.setParameters(parameters[0]);
        camera.autoFocus(new Camera.AutoFocusCallback() {
            @Override
            public void onAutoFocus(boolean success, Camera camera) {
                if (success) {
                    // 只有加上了这一句，才会自动对焦。
                    camera.cancelAutoFocus();
                    if (!Build.MODEL.equals("KORIDY H30")) {
                        parameters[0] = camera.getParameters();
                        parameters[0].setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);// 1连续对焦
                        camera.setParameters(parameters[0]);
                    }else{
                        parameters[0] = camera.getParameters();
                        parameters[0].setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
                        camera.setParameters(parameters[0]);
                    }
                }
            }
        });
    }
}
